package com.example.livesettingtest;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.example.livesettingtest.MyConstant;
import com.example.livesettingtest.R;

import floatWindow2.FloatActionController;
import floatWindow2.FloatPermissionManager;
import floatWindow2.rom.HuaweiUtils;
import floatWindow2.rom.MiuiUtils;
import floatWindow2.rom.RomUtils;

public class MainActivityForMI extends AppCompatActivity implements View.OnClickListener {

    String TAG = "KeepLivingSettings";
    RelativeLayout permissionSettingsLayout;
    RelativeLayout lockClearSettingsLayout;
    RelativeLayout notifySettingsLayout;
    RelativeLayout batteryOptionSettingsLayout;
    RelativeLayout wlanConnectSetting;
    RelativeLayout multitaskSetting;
    Button btTest;
    TextView tvHelpKeepLive;
    int mBrandId; //品牌ID
    boolean mIsPermissionFloat = false;
    boolean isBelowAndroid8 = false;
    int mOperation = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.xiaomi_keeplive_setting);
        super.onCreate(savedInstanceState);
        getLastIntentData();
        initView();
        String brand = (android.os.Build.BRAND);  //品牌系列，如华为公司的 huawei,honor

        String lowerLetterBrand = CommonUtil.getMobileBrandId();  //品牌
        Toast.makeText(this, lowerLetterBrand, Toast.LENGTH_LONG).show();
        Log.i(TAG,"brand="+lowerLetterBrand);
        Toast.makeText(this, lowerLetterBrand, Toast.LENGTH_LONG).show();
    }
    @Override
    protected void onResume() {
        Log.i(TAG,"Service MainActivity onResume mOperation="+mOperation);
        FloatActionController.getInstance().stopMonkServer(this);
        //gxw:其实这里isPermission返回false时并不代表用户没有打开权限原因未知，返回true那绝对是打开了权限。
        mIsPermissionFloat = FloatPermissionManager.getInstance().applyFloatWindow(this);
        super.onResume();
    }

    public void getLastIntentData()
    {
        Intent i = this.getIntent();
        Bundle b = i.getExtras();

        if(b != null) {
            mBrandId = b.getInt("brandId",-1);
        }
    }
    private void initView() {
        btTest = this.findViewById(R.id.btTest);
        permissionSettingsLayout = this.findViewById(R.id.permissionSettingsLayout);
        notifySettingsLayout = this.findViewById(R.id.notifySettingsLayout);
        batteryOptionSettingsLayout = this.findViewById(R.id.batteryOptionSettingsLayout);
        wlanConnectSetting = this.findViewById(R.id.wlanConnectSetting);
        multitaskSetting = this.findViewById(R.id.multitaskSetting);
        tvHelpKeepLive = this.findViewById(R.id.tvHelpKeepLive);
        lockClearSettingsLayout = this.findViewById(R.id.lockScreenClearLayout);

        permissionSettingsLayout.setOnClickListener(this);
        notifySettingsLayout.setOnClickListener(this);
        batteryOptionSettingsLayout.setOnClickListener(this);
        wlanConnectSetting.setOnClickListener(this);
        multitaskSetting.setOnClickListener(this);
        tvHelpKeepLive.setOnClickListener(this);
        lockClearSettingsLayout.setOnClickListener(this);
        btTest.setOnClickListener(this);

        //判断系统版本是否为7.0及以下.
        if(Build.VERSION.SDK_INT < 26)
        {
            lockClearSettingsLayout.setVisibility(View.VISIBLE);
            isBelowAndroid8 = true;
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.permissionSettingsLayout:
                mOperation = 0;
                MiuiUtils.getInstance(this).openPermission(MyConstant.tipType.MI_PERMISSION_MANAGE,mIsPermissionFloat);
                break;
            case R.id.lockScreenClearLayout:
                mOperation = 2;
                MiuiUtils.getInstance(this).lockScreenProtected(mIsPermissionFloat);
                break;
            case R.id.batteryOptionSettingsLayout:
                mOperation = 4;
                HuaweiUtils.getInstance(this).openIgnorBattery(mIsPermissionFloat);
                break;
            case R.id.multitaskSetting:
                mOperation = 6;
               /* Intent i = new Intent(this,MultiTaskReadMe.class);
                startActivity(i);*/
                break;
            case R.id.tvHelpKeepLive:
                mOperation = 7;
                gotoKeepLiveHelpDoc();
                break;
            case R.id.btTest:
                test();
                break;

/*            case R.id.autoRestartSettingsLayout:
                mOperation = 1;
                HuaweiUtils.getInstance(this).openAutoStart(isBelowAndroid8,mIsPermissionFloat);
                break;
            case R.id.notifySettingsLayout:
                mOperation = 3;
                HuaweiUtils.getInstance(this).openNotifySetting(isBelowAndroid8,mIsPermissionFloat);
                break;

            case R.id.wlanConnectSetting:
                mOperation = 5;
                HuaweiUtils.getInstance(this).openWlanSetting(mIsPermissionFloat);
                break;

        */
        }
    }


    /**
     * 引导用户
     */
    public void test() {
        String mtype = android.os.Build.MODEL; //型号
        Intent intent = new Intent();
        //gxw- intent.setData(Uri.parse("package:" + this.getPackageName()));

        //gxw-intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);//打开浮动窗口权限
        ComponentName componentName = null;
        componentName = new ComponentName("com.miui.securitycente", "com.miui.securitycente.com.miui.appmanager.ApplicationsDetailsActivity");
        //gxw-  intent.setComponent(componentName);
        //gxw-intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);//这个感觉不是系统最新的页面
        //gxw-intent.setAction(Settings.ACTION_APPLICATION_SETTINGS); //这个感觉不是系统最新的页面
        intent.setAction(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
        //gxw- intent.setAction(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
        String rom = getMiuiVersion();
        Toast.makeText(this,"rom="+rom+",mode="+mtype,Toast.LENGTH_LONG).show();

        try {
            this.startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(MainActivityForMI.this, "跳转异常" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
  /*      if ("V6".equals(rom) || "V7".equals(rom)) {
            intent.setAction("miui.intent.action.APP_PERM_EDITOR");
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
            intent.putExtra("extra_pkgname", packageName);
        } else if ("V8".equals(rom) || "V9".equals(rom)) {
            intent.setAction("miui.intent.action.APP_PERM_EDITOR");
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            intent.putExtra("extra_pkgname", packageName);
        } else {
            intent = getAppDetailsSettingsIntent(packageName);


            return;
        }*/
    }



    /**
     * 获取小米 rom 版本号，获取失败返回 -1
     *
     * @return miui rom version code, if fail , return -1
     */
    public  String getMiuiVersion() {
        String version = RomUtils.getSystemProperty("ro.miui.ui.version.name");
        if (version != null) {
            try {
                return version;
            } catch (Exception e) {
                Log.e(TAG, "get miui version code error, version : " + version);
                Log.e(TAG, Log.getStackTraceString(e));
            }
        }
        return version;
    }

   public void gotoKeepLiveHelpDoc()
    {
        Log.i(TAG,"brandId = "+mBrandId);
        String live_setting = this.getResources().getString(R.string.live_setting);
        String appLiveUrl = CommonUtil.getAppLiveSettingUrl(mBrandId);

        Intent liveSettings = new Intent(MainActivityForMI.this,WebViewBrowser.class);
        liveSettings.putExtra("url",appLiveUrl);
        liveSettings.putExtra("title",live_setting);
        this.startActivity(liveSettings);
    }
}
